package com.admin.system.controller;

import cn.hutool.core.lang.tree.Tree;
import com.admin.model.base.PageResult;
import com.admin.model.dto.MenuDto;
import com.admin.system.service.MenuService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.groups.Default;
import java.util.List;
import java.util.Set;

/**
 * (Menu)表控制层
 *
 * @author makejava
 * @since 2021-01-31 20:10:18
 */
@Api(tags = "菜单")
@RestController
@RequestMapping("menu")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;

    @ApiOperation("创建")
    @ApiOperationSupport(ignoreParameters = {"id", "createTime", "updateTime", "createdBy", "updatedBy", "children", "hasChildren", "createdUser", "updatedUser"})
    // @PreAuthorize("hasAnyAuthority('menu:add')")
    @PostMapping
    public ResponseEntity<Object> create(@Validated({MenuDto.Create.class, Default.class}) @RequestBody MenuDto menuDto) {
        menuService.create(menuDto);
        return ResponseEntity.ok("创建成功");
    }

    @ApiOperation("id查询")
    @PreAuthorize("hasAnyAuthority('menu:list')")
    @GetMapping("/{id}")
    public ResponseEntity<Object> findById(@PathVariable Long id) {
        MenuDto menuDto = menuService.findById(id);
        return ResponseEntity.ok(menuDto);
    }


    @ApiOperation("ids删除")
    @PreAuthorize("hasAnyAuthority('menu:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteByIds(@RequestBody Set<Long> ids) {
        menuService.deleteByIds(ids);
        return ResponseEntity.ok("删除成功");
    }

    @ApiOperation("条件查询")
    @ApiOperationSupport(ignoreParameters = {"children", "hasChildren", "createdUser", "updatedUser"})
    @PreAuthorize("hasAnyAuthority('menu:list')")
    @PostMapping("/search")
    public ResponseEntity<Object> findAll(@RequestBody(required = false) MenuDto menuDto) {
        List<MenuDto> menuDtos = menuService.queryAll(menuDto);
        return ResponseEntity.ok(menuDtos);
    }

    @ApiOperation("菜单树")
    @PreAuthorize("hasAnyAuthority('menu:list')")
    @GetMapping("/tree")
    public ResponseEntity<Object> findTree() {
        List<Tree<String>> trees = menuService.buildTree();
        return ResponseEntity.ok(trees);
    }

    @ApiOperation("id更新")
    @ApiOperationSupport(ignoreParameters = {"createTime", "updateTime", "createdBy", "updatedBy", "children", "hasChildren", "createdUser", "updatedUser"})
    @PreAuthorize("hasAnyAuthority('menu:edit')")
    @PutMapping
    public ResponseEntity<Object> updateById(@Validated({MenuDto.Update.class, Default.class}) @RequestBody MenuDto menuDto) {
        menuService.updateById(menuDto);
        return ResponseEntity.ok("更新成功");
    }

    @ApiOperation("pid查询")
    @PreAuthorize("hasAnyAuthority('menu:list')")
    @GetMapping("/par/{pid}")
    public ResponseEntity<Object> findByPid(@PathVariable Long pid) {
        List<MenuDto> menuDtoList = menuService.queryByPid(pid);
        return ResponseEntity.ok(menuDtoList);
    }


}